package com.weiai.wedding.service.user;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.weiai.wedding.dao.user.AuthDao;
import com.weiai.wedding.dao.user.UserDao;
import com.weiai.wedding.entity.user.Auth;
import com.weiai.wedding.entity.user.User;
import com.weiai.wedding.service.ServiceException;
import com.weiai.wedding.service.ShiroDbRealm;
import com.weiai.wedding.service.ShiroDbRealm.ShiroUser;
import com.weiai.wedding.util.ConUtils;
import com.weiai.wedding.util.Constants;
import com.weiai.wedding.util.MD5;
import com.weiai.wedding.util.StatusConstants;
/**
 * 用户管理类
 * @author wangweiheng
 *
 */
@Component
public class UserService {
	@Autowired
	private UserDao userDao;
	@Autowired
	private AuthDao authDao;
	@Autowired
	private ShiroDbRealm shiroRealm;
	public User findUser(String id){
		return userDao.findOne(id);
	}
	/**
	 * 保存用户信息
	 * @param user
	 */
	public void saveUser(User user){
		String authCodeString = user.getAuthCodeString();
		String[] authArray = authCodeString.split(",");
		List<String> authList = new ArrayList<String>();
		//增加权限
		for(int i=0;i<authArray.length;i++){
			authList.add(authArray[i]);
		}
		user.setAuthList(authList);
		if(StringUtils.isBlank(user.getId())){
			user.setPassword(MD5.MD5Encode(user.getPassword()));
			user.setStatus(StatusConstants.AUDITED);
		}else{
			User u=userDao.findOne(user.getId());
			user.setPassword(u.getPassword());
			user.setStatus(u.getStatus());
		}
		userDao.save(user);
		shiroRealm.clearAllCachedAuthorizationInfo();
	}
	
	public User findUserByLoginName(String loginName){
		return userDao.findByLoginName(loginName);
	}
	
	@Transactional(readOnly = false)
	public void setUserAuth(String loginName) {
		User user=this.findUserByLoginName(loginName);
		if(user!=null){
			List<Auth> list=authDao.findAll();
			List<String> sList=user.getAuthList();
			int authNum=list.size();
			int userAuthNum=0;
			if(sList!=null && sList.size()>0){
				userAuthNum=sList.size();
			}
			//如果admin的权限和总权限不同，则重新设置权限
			if(authNum!=userAuthNum){
				List<String> aList=new ArrayList<String>();
				for(Auth auth:list){
					aList.add(auth.getAuthCode());
				}
				user.setAuthList(aList);
				userDao.save(user);
				shiroRealm.clearAllCachedAuthorizationInfo();
			}
		}
	}
	
	/**
	 * 查询用户信息列表
	 * @param
	 * @return
	 */
	public Page<User> findUserAll(Pageable pager){
		//begin 查找代理商下的用户
		Page<User> page = userDao.findAllUser(pager);
			
		return page;
	}
	public Page<User> findUserAllByLoginName(String loginName, Pageable pageable) {
		// TODO Auto-generated method stub
		return userDao.findUserAllByLoginName(loginName,pageable);
	}
	@Transactional(readOnly = false)
	public void deleteUser(String id){
		User user = this.findUser(id);
		if("admin".equals(user.getLoginName())){
			//logger.warn("操作员{}尝试删除超级管理员用户", SecurityUtils.getSubject().getPrincipal());
			throw new ServiceException("不能删除超级管理员用户");
		}
		user.setStatus(StatusConstants.DELETE_STATUS);
		userDao.save(user);
		shiroRealm.clearAllCachedAuthorizationInfo();
	}
}
